package com.hujifang.elasticsearch.utils;

import com.chenlb.mmseg4j.analysis.MMSegAnalyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Lucene工具类
 * Created by Hurricane.Hu on 2016/7/25.
 */
public class LuceneUtil {
	private static MMSegAnalyzer mmSegAnalyzer=new MMSegAnalyzer();
	/**
	 * mmseg4j分词
	 * @param texts
	 */
	public static List<String> analysisWordMmseg4j(String ... texts){
		List<String> termList=new ArrayList<>();
		for (String text:texts){
			try {
				StringReader reader = new StringReader(text);
				TokenStream ts = mmSegAnalyzer.tokenStream("", reader);
				CharTermAttribute term = ts.getAttribute(CharTermAttribute.class);
				while(ts.incrementToken()){
					termList.add(term.toString());
				}
			} catch (IOException ignore) {
			}
		}
		return termList;
	}

	public static void main(String[] args) {
		System.err.println(analysisWordMmseg4j("如果我使用了邀请码下单，但是没有付款，取消了订单，之后再使用邀请码，还有效吗？"));
	}
}
